//
//  YZMAnnDetailModel.m
//  YZMicroStock
//
//  Created by Lv.Dongdong on 16/4/29.
//  Copyright © 2016年 cqjr. All rights reserved.
//

#import "YZMAnnDetailModel.h"
#import "Mantle+Utils.h"

@implementation YZMAnnDetailModel

+ (NSDictionary *)JSONKeyPathsByPropertyKey {
    
    return @{
             @"noticeContent": @"noticeContent",
             @"noticeTitle": @"noticeTitle",
             @"noticeType": @"noticeType",
             @"releaseTime": @"releaseTime"
             };
}

+ (NSValueTransformer *)releaseTimeJSONTransformer {
    
    return [NSValueTransformer valueTransformerForName: MTLDoubleDateTransformerName];
}

+ (void)createEncoder:(YZMAnnDetailModel *)dataModel forKey:(NSString *)keyName {
    
    if (!dataModel || !keyName) {
        
        return;
    }
        
    NSMutableData *data = [NSMutableData data];
    //创建一个归档类
    NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:data];
    [archiver encodeObject:data forKey:keyName];
    [archiver finishEncoding];

    //将数据写入文件里
    [[NSUserDefaults standardUserDefaults] setObject:data forKey:keyName];
    [[NSUserDefaults standardUserDefaults] synchronize];
}

- (void)encodeWithCoder:(NSCoder *)aCoder //将属性进行编码
{
    [aCoder encodeObject:self.noticeContent forKey:@"noticeContent"];
    [aCoder encodeObject:self.noticeTitle forKey:@"noticeTitle"];
    [aCoder encodeInt:self.noticeType forKey:@"noticeType"];
    [aCoder encodeObject:self.releaseTime forKey:@"releaseTime"];
}


- (id)initWithCoder:(NSCoder *)aDecoder //将属性进行解码
{
    self = [super init];
    
    if (self) {
        
        self.noticeContent = [aDecoder decodeObjectForKey:@"noticeContent"];
        self.noticeTitle = [aDecoder decodeObjectForKey:@"noticeTitle"];
        self.noticeType = [aDecoder decodeIntForKey:@"noticeType"];
        self.releaseTime = [aDecoder decodeObjectForKey:@"releaseTime"];
    }
    
    return self;
}


- (void)createEncoderWithKeyName:(NSString *)keyName {
    
    NSMutableData *data = [NSMutableData data];
    //创建一个归档类
    NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc]initForWritingWithMutableData:data];
    [archiver encodeObject:self forKey:keyName];
    [archiver finishEncoding];
    //将数据写入文件里
    [[NSUserDefaults standardUserDefaults] setObject:data forKey:keyName];
    [[NSUserDefaults standardUserDefaults] synchronize];
}

+ (id)createDecoderWithKeyName:(NSString *)keyName {
    
    NSData *data = [[NSUserDefaults standardUserDefaults] objectForKey:keyName];
    
    if (data && data.length != 0) {
        
        NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:data];
        
        YZMAnnDetailModel *instance = [unarchiver decodeObjectForKey:keyName];
        [unarchiver finishDecoding];
        
        return instance;
    }
    
    return nil;
}

@end
